package config

import (
	"openapi.itspire.cn/common/global"
	"github.com/joho/godotenv"
	"gopkg.in/yaml.v2"
	"io/ioutil"
	"log"
	"os"
)

var Conf Yaml

type Yaml struct {
	App struct {
		AppName string `yaml:"appName"`
		LogDir  string `yaml:"logDir"`
		AESKey  string `yaml:"aesKey"`
	}
	Database struct {
		Driver   string `yaml:"driver"`
		Protocol string `yaml:"tcp"`
		Host     string `yaml:"host"`
		Port     int    `yaml:"port"`
		User     string `yaml:"user"`
		Password string `yaml:"password"`
		Name     string `yaml:"name"`
	}
	Redis struct {
		Driver   string `yaml:"driver"`
		Protocol string `yaml:"tcp"`
		Host     string `yaml:"host"`
		Port     int    `yaml:"port"`
		Password string `yaml:"password"`
	}
}

func init() {
	// 获取.env文件配置
	err := godotenv.Load(".env")
	if err != nil {
		log.Fatalf("Error loading .env file #%v ", err)
	}
	env := os.Getenv("ENV")
	// 配置运行环境
	global.Env = env
	// 读取配置文件
	yamlFile, err := ioutil.ReadFile(global.Env + ".yaml")
	if err != nil {
		log.Fatalf("yamlFile.Get err #%v ", err)
	}
	// 解析配置文件
	err = yaml.Unmarshal(yamlFile, &Conf)
	if err != nil {
		log.Fatalf("yaml Unmarshal: #%v", err)
	}
}
